var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var passport = require('passport');
// morgan中间件记录日志

var indexRouter = require('./routes/index');
var app = express();
var { morganMiddleware } = require('./utils/Middleware/morgan')


// swagger
require('./utils/swagger')(app, __dirname)

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(morganMiddleware);
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(passport.initialize());
app.use(passport.session());

for (const key in indexRouter) {
  if (Object.hasOwnProperty.call(indexRouter, key)) {
    const element = indexRouter[key];
    var appPath = key.replace(/Router/, "")
    app.use('/' + appPath, element);
  }
}

app.use(express.static(path.join(__dirname, 'public')));
app.use('/files', express.static(path.join(__dirname, 'files')));


module.exports = app;
